package gui.properties;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.MetroModel;
import model.objects.EdiLine;

import com.nightpiter.controller.log.Logger;
import com.nightpiter.model.objects.Line;

public class LinePropertiesPanel extends FlowPanel{
	private EdiLine line;
	private LineColorPreview preview;
	private ColorField colorField;
	
	public LinePropertiesPanel(EdiLine line) {
		super(line.getName());
		this.line = line;
		this.setPanelComponents(this.initPanelComponents());
		this.setPrVisible(true);
	}
	private List<Component> initPanelComponents() {
		List<Component> comps = new LinkedList<Component>();	
		
		JPanel namePanel = new JPanel();
		namePanel.setLayout(new GridLayout(0,2));
		namePanel.add(new JLabel("Name:"));
		namePanel.add(new NameField());
		comps.add(namePanel);
		
		JPanel delayPanel = new JPanel();
		delayPanel.setLayout(new GridLayout(0,2));
		delayPanel.add(new JLabel("Train delay:"));
		delayPanel.add(new DelayField());
		comps.add(delayPanel);
		
		JPanel colorPanel = new JPanel();
		colorPanel.setLayout(new GridLayout(0,2));
		JPanel colorSecondaryPanel = new JPanel();
		colorPanel.add(new JLabel("Color:"));
		colorSecondaryPanel.setLayout(new BoxLayout(colorSecondaryPanel, BoxLayout.Y_AXIS));
		colorField = new ColorField();
		
		JPanel colorThirdPanel = new JPanel();
		colorThirdPanel.setLayout(new BoxLayout(colorThirdPanel, BoxLayout.X_AXIS));
		preview = new LineColorPreview();		
		colorThirdPanel.add(colorField);
		colorThirdPanel.add(preview);
		colorSecondaryPanel.add(colorThirdPanel);
		colorSecondaryPanel.add(new PickColorButton());
		colorPanel.add(colorSecondaryPanel);
		comps.add(colorPanel);
		
		return comps;
	}
	
	@Override
	public void updateData() {
		Logger.log("ddddddd");
		super.updateData();
		line.getColor();
		colorField.setText(colorToHex(new Color(line.getColor())));
		preview.setColor(new Color(line.getColor()));
		
	}
	class NameField extends JTextField {
		public NameField() {
			//final JTextField nameField = this;
			setText(line.getName());		
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setLineName(line, getText());
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		
	}
	
	class DelayField extends JTextField {
		public DelayField() {
			//final JTextField nameField = this;
			setText(line.getTrainDelay()+"");		
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setLineDelay(line, Integer.parseInt(getText()));
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
		
	}
	
	class ColorField extends JTextField {
		public ColorField() {
			//final JTextField nameField = this;
			setText(colorToHex(new Color(line.getColor())));		
			
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setLineColor(line, Color.decode(getText()));
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
		}
	}
	
	private String colorToHex(Color color) {
		String c = "#";
		
		c+=numberToLetter(color.getRed()/16);
		c+=numberToLetter(color.getRed()%16);
		c+=numberToLetter(color.getGreen()/16);
		c+=numberToLetter(color.getGreen()%16);
		c+=numberToLetter(color.getBlue()/16);
		c+=numberToLetter(color.getBlue()%16);
		return c;
	}
	
	private char numberToLetter(int num) {
		
		switch(num) {
		case 0:
			return '0';
		case 1:
			return '1';
		case 2:
			return '2';
		case 3:
			return '3';
		case 4:
			return '4';
		case 5:
			return '5';
		case 6:
			return '6';
		case 7:
			return '7';
		case 8:
			return '8';
		case 9:
			return '9';
		case 10:
			return 'A';
		case 11:
			return 'B';
		case 12:
			return 'C';
		case 13:
			return 'D';
		case 14:
			return 'E';
		case 15:
			return 'F';
		}
		return '-';
	}
	
	class PickColorButton extends JButton {
		public PickColorButton() {
			super("pick color");
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					final JColorChooser colorChooser = new JColorChooser(new Color(line.getColor()));
					ActionListener onOk = new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {
							MetroModel.getInstance().setLineColor(line, colorChooser.getColor());
							
						}
						
					};
					ActionListener onCancel = new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {							
							//do nothing
						}
						
					};
					
					JColorChooser.createDialog(prPanel, "Choose line color", true, colorChooser, onOk, onCancel).setVisible(true);
					

				}
				
			});
			//final JTextField nameField = this;
			//setText(line.getColor()+"");		
		}		
	}
	
	class LineColorPreview extends JPanel {
		private Color color = new Color(line.getColor());
		public void setColor(Color color) {
			this.color = color;
			repaint();
		}
		LineColorPreview() {
			this.setPreferredSize(new Dimension(20,16));
			this.setMaximumSize(new Dimension(10,22));
			this.setMinimumSize(new Dimension(10,10));
			/*lineBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {					
					color = new Color(((Line)lineBox.getSelectedItem()).getColor());
					repaint();
				}
				
			});*/
		}
		
		@Override		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(color);
			g.fillRect(0, 2, this.getWidth()-3, getHeight()-5);
			
		}
	}

	
}
